package com.thermal.thermal_inspection.Listener;

import com.thermal.thermal_inspection.mapper.StatisticsUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
public class StartUpListener {
    @Autowired
    private StatisticsUserMapper statisticsUserMapper;

    @EventListener(ContextRefreshedEvent.class)
    public void onApplicationEvent() {
        checkAndInsertStatisticsUser();
    }

    public void checkAndInsertStatisticsUser() {
        LocalDate today = LocalDate.now();
        // 检查 statistic_user 表中是否已经存在今天的记录
        boolean hasTodayData = statisticsUserMapper.existsTodayData(today);

        if (!hasTodayData) {
            // 如果今天的数据不存在，插入新记录
            statisticsUserMapper.insertAllUserUnit();
        }
    }
}
